package hos.table.simple;

import android.text.Spanned;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.text.HtmlCompat;

import com.evrencoskun.tableview.ITableView;
import com.evrencoskun.tableview.sort.SortState;

import hos.table.BaseTableAdapter;
import hos.table.R;
import hos.table.holder.BaseTableSorterHolder;
import hos.table.model.ColumnHeader;


public class SimpleColumnHeaderViewHolder extends BaseTableSorterHolder<ColumnHeader> {
    private AppCompatImageView ranking;

    public SimpleColumnHeaderViewHolder(@NonNull SimpleTableAdapter tableView, @NonNull View itemView) {
        this(tableView, itemView, false);
    }

    public SimpleColumnHeaderViewHolder(@NonNull SimpleTableAdapter tableView, @NonNull View itemView, boolean isRanking) {
        super(itemView);
        AppCompatTextView value = itemView.findViewById(R.id.value);
        View root = itemView.findViewById(R.id.root);
        if (isRanking) {
            ranking = itemView.findViewById(R.id.ranking);
            root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (SimpleColumnHeaderViewHolder viewHolder : tableView.getHeaderList()) {
                        if (viewHolder != SimpleColumnHeaderViewHolder.this) {
                            viewHolder.resetSort();
                        }
                    }
                    if (getSortState() == SortState.ASCENDING) {
                        tableView.getTableView().sortColumn(getBindingAdapterPosition(), SortState.DESCENDING);
                    } else if (getSortState() == SortState.DESCENDING) {
                        tableView.getTableView().sortColumn(getBindingAdapterPosition(), SortState.ASCENDING);
                    } else {
                        // Default one
                        tableView.getTableView().sortColumn(getBindingAdapterPosition(), SortState.DESCENDING);
                    }
                    tableView.getTableView().scrollToRowPosition(0);
                }
            });
        }
    }

    @Override
    public void onConvert(@NonNull BaseTableAdapter adapter, @NonNull View itemView, @Nullable ColumnHeader model, int columnPosition, int rowPosition) {
        if (model == null) {
            return;
        }
        Object content = model.getContent();
        AppCompatTextView value = itemView.findViewById(R.id.value);
        if (content instanceof Spanned) {
            value.setText((Spanned) model.getContent());
        } else if (content instanceof CharSequence) {
            value.setText((CharSequence) model.getContent());
        } else {
            String contains = String.valueOf(content);
            if (contains.contains(">") || contains.contains("<")) {
                value.setText(HtmlCompat.fromHtml(contains, HtmlCompat.FROM_HTML_MODE_LEGACY));
            } else {
                value.setText(String.valueOf(model.getContent()));
            }
        }
        View dividerRight = itemView.findViewById(R.id.dividerRight);
        View dividerBottom = itemView.findViewById(R.id.dividerBottom);
        ITableView tableView = adapter.getTableView();
        int separatorColor = tableView.getSeparatorColor();
        dividerRight.setBackgroundColor(separatorColor);
        dividerBottom.setBackgroundColor(separatorColor);
        dividerRight.setVisibility(View.GONE);
        if (tableView.isShowVerticalSeparators()) {
            dividerBottom.setVisibility(View.VISIBLE);
        } else {
            dividerBottom.setVisibility(View.GONE);
        }
        itemView.setBackgroundResource(R.color.table_header_bg);
    }


    @Override
    public void setBackgroundColor(int p_nColor) {

    }

    public void resetSort() {
        onSortingStatusChanged(SortState.UNSORTED);
    }

    @Override
    public void onSortingStatusChanged(@NonNull SortState pSortState) {
        super.onSortingStatusChanged(pSortState);
        if (ranking == null) {
            return;
        }
        if (getSortState() == SortState.ASCENDING) {
            ranking.setSelected(true);
            ranking.setActivated(false);
        } else if (getSortState() == SortState.DESCENDING) {
            ranking.setSelected(false);
            ranking.setActivated(true);
        } else {
            // Default one
            ranking.setSelected(false);
            ranking.setActivated(false);
        }
        itemView.requestLayout();
    }
}
